<template>
    <div>
        <H1>科室新增</H1>
        <table class="table table-bordered" style="background-color:cadetblue;">
            <tbody>
                <tr>
                    <td>科室名称</td>
                    <td><input type="text" v-model="info.name"></td>
                </tr>
                  <tr>
                    <td>科室人数</td>
                    <td><input type="text" v-model="info.people"></td>
                </tr>
                 <tr>
                    <td>科室介绍</td>
                    <td><textarea v-model="info.introduce"></textarea></td>
                </tr>
                <tr>
                    <td>科室</td>
                    <td> <select v-model="info.lid">
            <option value="">请选择</option>
            <option :value="item.oid" v-for="item in level" :key="item.oid">{{item.oname}}</option>  
        </select></td>
                </tr>
                <tr>
                    <td>创建时间</td>
                    <td><input type="date" v-model="info.createTime"></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="button" value="添加" @click="add"></td>
                </tr>
            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import axios from 'axios';
import { onMounted } from 'vue';
onMounted(()=>{
    GetLevel();
})


let level:any=ref([]);
let info:any=ref({
  "id": 0,
  "name": "",
  "people": "",
  "isdel": false,
  "introduce": "",
  "createTime": "",
  "lid": 0
})

const GetLevel=()=>{
      axios({
        url:'https://localhost:7118/api/Office/GetOfficeLevels',
        method:'get',
    })
    .then((res)=>[
        console.log(res),
        level.value=res.data
    ])
    .catch((err)=>{
        console.log(err);
    })
}

const add=()=>{
    if(info.value.name==""){
        alert("科室名称不能为空")
        return;
    }
      if(info.value.people==""){
        alert("科室人数不能为空")
        return;
    }
      if(info.value.introduce==""){
        alert("科室介绍不能为空")
        return;
    }
    axios({
        url:'https://localhost:7118/api/Office/AddOffice',
        method:'post',
        data:info.value
    })
    .then((res)=>{
        console.log(res);
        if(res.data==-1){
            alert("科室名称重复了!");
            return;
        }
        alert("添加成功");
        location.href='/officeshow'
    })
    .catch((err)=>{
        console.log(err);
    })
}

</script>

<style scoped>

</style>